package com.mijie.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class ZipUtil {

	private static Logger logger = Logger.getLogger(ZipUtil.class);
	/**
	 * ZipUtil.decompression("/Users/zhengzhibin/Downloads/","bin.zip", "/Users/zhengzhibin/Downloads/");
	 * @param srcPath
	 * @param zipName
	 * @param destPath 保存到哪个目录下
	 * @param rename true则使用系统时间做文件名
	 * @return
	 */
	public static List<File> decompression(String srcPath,String zipName,String destPath,boolean rename){
		List<File> succDecompressFilelist = new ArrayList<File>();
		byte[] tmp = new byte[2048];  
		InputStream is = null;
		BufferedInputStream bis = null;
		ZipEntry entry = null;
		File file = null;
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		String dirName;
		String suffix = null;
		int index = -1;
		String prefix = null;
		ZipFile zip = null;
		
		try {
			if(!srcPath.endsWith(File.separator)) srcPath += File.separator;
			if(!destPath.endsWith(File.separator)) destPath+=File.separator;
			System.out.println("zip file->"+srcPath+zipName);
			
			zip =  new ZipFile(srcPath+zipName);
			
			@SuppressWarnings("unchecked")
			Enumeration<ZipEntry> entries = zip.getEntries();
			while(entries.hasMoreElements()){
				entry = entries.nextElement();
				if(entry.isDirectory()) continue;
				if(rename) {
					index = entry.getName().lastIndexOf(File.separator);
					if(index==-1){
						dirName = "";
						index = entry.getName().lastIndexOf(".");
						if(index==-1) suffix = "";
						else suffix = entry.getName().substring(index);
					}else{
						dirName = entry.getName().substring(0, index+1);
						suffix = entry.getName().substring(index);
						index = suffix.lastIndexOf(".");
						if(index==-1) suffix = "";
						else suffix = suffix.substring(suffix.lastIndexOf("."));
					}
					file = new File(destPath,dirName+System.nanoTime()+suffix);
				}else{
					file = new File(destPath,entry.getName());
				}
				if(!file.exists())
					new File(file.getParent()).mkdirs();
				if(!file.createNewFile()){
					logger.debug("###file create fail -> "+file.getAbsolutePath());
					continue;
				}
				if(logger.isDebugEnabled())
					logger.debug("###extract file -> "+file.getAbsolutePath());
				
				fos = new FileOutputStream(file);
				bos =  new BufferedOutputStream(fos);  
		        is =  zip.getInputStream(entry);
		        bis =  new BufferedInputStream(is);
		        while(bis.read(tmp)!= -1){  
		        	bos.write(tmp);  
		        }
		        if(bos!=null) bos.close();
				if(fos!=null) fos.close();
		       succDecompressFilelist.add(file);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}finally{
			try {
				if(bis!=null) bis.close();
				if(is!=null) is.close();
				if(zip!=null) zip.close();
			} catch (IOException e) {} 
		}
		return succDecompressFilelist;
	}
	
	
	public static void main(String[] args){
		ZipUtil.decompression("/Users/zhengzhibin/Downloads/","0000.zip", "/Users/zhengzhibin/Downloads/test/",true);
	}
}
